/**
 * 
 */
package com.estock.web.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.estock.common.model.security.Stock;
import com.estock.common.model.security.StockCategory;
import com.estock.common.util.support.ESLogger;
import com.estock.web.service.StockCategoryService;
import com.estock.web.service.StockService;

/**
 * @author richard
 * 
 */
@Controller
public class StockCategoryController {

	protected final ESLogger logger = ESLogger.getLogger(StockCategoryController.class);
	
	@Resource
	StockCategoryService stockCategoryService; 

	public StockCategoryService getStockCategoryService() {
		return stockCategoryService;
	}
	
	@Resource
	StockService stockService; 

	public StockService getStockService() {
		return stockService;
	}
	
	//////// Return View

	//////// Return Json
	
	@RequestMapping(value="findCategoriesByStockId.es", method = RequestMethod.GET)
	@ResponseBody
	public List<StockCategory> findCategoriesByStockId(@RequestParam int stockId) throws Exception {
		logger.info("request url: /findCategoriesByStockId.es with parameter stockId = " + stockId);
		return getStockCategoryService().findCategoriesByStockId(stockId);
	}
	
	@RequestMapping(value="findCategoriesByStockCode.es", method = RequestMethod.GET)
	@ResponseBody
	public List<StockCategory> findCategoriesByStockCode(@RequestParam String code) throws Exception {
		logger.info("request url: /getCategoriesByStockCode.es with parameter code = " + code);
		return getStockCategoryService().findCategoriesByStockCode(code);
	}
	
	@RequestMapping(value="saveStockCategoryWithCode.es", method = RequestMethod.GET)
	@ResponseBody
	public StockCategory saveStockCategoryWithCode(@RequestParam String code, @RequestParam String categoryName) throws Exception {
		StockCategory category = saveStockCategory(categoryName);
		
		Stock stock = getStockService().findStockByCode(code);
		if (!category.getStocks().contains(stock)) {
			category.addStock(stock);
			// persistence should occur at category side.
			getStockCategoryService().saveOrUpdate(category);
		}
		
		return category;
	}
	
	@RequestMapping(value="saveStockCategory.es", method = RequestMethod.GET)
	@ResponseBody
	public StockCategory saveStockCategory(@RequestParam String categoryName) throws Exception {
		logger.info("start saveStockCategory with name: %s", categoryName);
		if (StringUtils.isBlank(categoryName)) {
			throw new RuntimeException("category name cannot be empty or blank!");
		}
		
		// create new one if can not found from DB.
		StockCategory category = getStockCategoryService().findStockCategoryByName(categoryName);
		if (category == null) {
			category = new StockCategory(categoryName, 1);
			// persistence should occur at category side.
			getStockCategoryService().saveOrUpdate(category);
		}
		
		return category;
	}
	
	@RequestMapping(value="deleteStockCategory.es", method = RequestMethod.GET)
	@ResponseBody
	public String deleteStockCategory(@RequestParam long stockCategoryId) throws Exception {
		logger.info("start deleting stock category with id: %s", stockCategoryId);
		getStockCategoryService().deleteById(stockCategoryId);
		return "success";
	}
	
	@RequestMapping(value="deleteStockCatRelate.es", method = RequestMethod.GET)
	@ResponseBody
	public String deleteStockCatRelate(@RequestParam long stockCategoryId, @RequestParam long stockId) throws Exception {
		logger.info("start deleting stockCatRelate with category: %s and stockId: %s", stockCategoryId, stockId);
		
		StockCategory category = getStockCategoryService().findById(stockCategoryId);
		Stock stock = getStockService().findById(stockId);
		if (category.getStocks().contains(stock)) {
			category.removeStock(stock);
			// persistence should occur at category side.
			getStockCategoryService().saveOrUpdate(category);
		}
		
		return "success";
	}
	
	@RequestMapping(value="findAllStockCategoies.es", method = RequestMethod.GET)
	@ResponseBody
	public List<StockCategory> findAllActiveStockCategoies() throws Exception {
		logger.info("start finding all stock categoies");
		return getStockCategoryService().findAllActiveStockCategoies();
	}
	
}
